package com.example.teesystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.PreCourseScore;
import com.example.teesystem.entity.vo.preCourse.PreCourseGet;
import com.example.teesystem.entity.vo.preCourseScore.PreCourseResponse;
import com.example.teesystem.entity.vo.preCourseScore.PreCourseScoreInsert;
import com.example.teesystem.entity.vo.preCourseScore.ScoreInsert;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.mapper.PreCourseMapper;
import com.example.teesystem.service.PreCourseScoreService;
import com.example.teesystem.mapper.PreCourseScoreMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author 12510
* @description 针对表【t_pre_course_score】的数据库操作Service实现
* @createDate 2022-05-27 11:15:24
*/
@Service
@AllArgsConstructor
public class PreCourseScoreServiceImpl extends ServiceImpl<PreCourseScoreMapper, PreCourseScore>
    implements PreCourseScoreService{

    private final PreCourseScoreMapper preCourseScoreMapper;
    private final PreCourseMapper preCourseMapper;

    @Override
    @Transactional
    public Response insertOrUpdatePreCourseScore(ScoreInsert scoreInsert) {
        try{
            List<Integer> integerList = preCourseMapper.getIds(scoreInsert.getCourseId());
            for (PreCourseScoreInsert courseScore : scoreInsert.getPreCourseScoreInserts()) {
                if(!integerList.contains(courseScore.getPreCourse())){
                    return Response.error(ResultCode.PARAMETER_ERROR);
                }
                Integer id = preCourseScoreMapper.getPreCourseScore(MyHandlerInterceptor.getUid(), courseScore.getPreCourse());
                if (id == null) {
                    PreCourseScore preCourseScore = new PreCourseScore();
                    preCourseScore.setPreCourse(courseScore.getPreCourse());
                    preCourseScore.setScore(courseScore.getScore());
                    preCourseScore.setStudentId(MyHandlerInterceptor.getUid());
                    preCourseScore.setCourseId(scoreInsert.getCourseId());
                    preCourseScoreMapper.insert(preCourseScore);
                } else {
                    preCourseScoreMapper.updatePreCourseScore(courseScore.getScore(), id);
                }

            }
            return Response.ok();
        }catch (Exception e){
            return Response.error(ResultCode.UPDATE_MESSAGE_ERROR);
        }
    }

    @Override
    public Response getPreCourseScore(int courseId) {
        List<PreCourseGet> preCourseGetList = preCourseMapper.getPreCourse(courseId);
        List<PreCourseScore> preCourseScoreList = preCourseScoreMapper.getPreCourseScores(MyHandlerInterceptor.getUid(), courseId);
        List<PreCourseResponse> preCourseResponseList = new ArrayList<>();
        for (PreCourseGet preCourseGet : preCourseGetList) {
            PreCourseResponse preCourseResponse = new PreCourseResponse();
            preCourseResponse.setPreCourse(preCourseGet.getId());
            preCourseResponse.setScore(0);
            preCourseResponse.setPreCourseName(preCourseGet.getCourseName());
            for (PreCourseScore preCourseScore : preCourseScoreList) {
                if (preCourseGet.getId().equals(preCourseScore.getPreCourse())) {
                    preCourseResponse.setId(preCourseScore.getId());
                    preCourseResponse.setScore(preCourseScore.getScore());
                }
            }
            preCourseResponseList.add(preCourseResponse);
        }
        return Response.ok(preCourseResponseList);
    }

    @Override
    public Response getStuPreCourseScore(int studentId, int courseId) {
        List<PreCourseGet> preCourseGetList = preCourseMapper.getPreCourse(courseId);
        List<PreCourseScore> preCourseScoreList = preCourseScoreMapper.getPreCourseScores(studentId, courseId);
        List<PreCourseResponse> preCourseResponseList = new ArrayList<>();
        for (PreCourseGet preCourseGet : preCourseGetList) {
            PreCourseResponse preCourseResponse = new PreCourseResponse();
            preCourseResponse.setPreCourse(preCourseGet.getId());
            preCourseResponse.setScore(0);
            preCourseResponse.setPreCourseName(preCourseGet.getCourseName());
            for (PreCourseScore preCourseScore : preCourseScoreList) {
                if (preCourseGet.getId().equals(preCourseScore.getPreCourse())) {
                    preCourseResponse.setId(preCourseScore.getId());
                    preCourseResponse.setScore(preCourseScore.getScore());
                }
            }
            preCourseResponseList.add(preCourseResponse);
        }
        return Response.ok(preCourseResponseList);
    }

    @Override
    public Response updateStuPreCourseScore(int id, int score) {
        try{
            preCourseScoreMapper.updatePreCourseScore(score, id);
            return Response.ok();
        }catch (Exception e){
            return Response.error(ResultCode.UPDATE_MESSAGE_ERROR);
        }
    }

    @Override
    public List<PreCourseResponse> getStudentPreCourseScore(int studentId, int courseId) {
        List<PreCourseGet> preCourseGetList = preCourseMapper.getPreCourse(courseId);
        List<PreCourseScore> preCourseScoreList = preCourseScoreMapper.getPreCourseScores(studentId, courseId);
        List<PreCourseResponse> preCourseResponseList = new ArrayList<>();
        for (PreCourseGet preCourseGet : preCourseGetList) {
            PreCourseResponse preCourseResponse = new PreCourseResponse();
            preCourseResponse.setPreCourse(preCourseGet.getId());
            preCourseResponse.setScore(0);
            preCourseResponse.setPreCourseName(preCourseGet.getCourseName());
            for (PreCourseScore preCourseScore : preCourseScoreList) {
                if (preCourseGet.getId().equals(preCourseScore.getPreCourse())) {
                    preCourseResponse.setId(preCourseScore.getId());
                    preCourseResponse.setScore(preCourseScore.getScore());
                }
            }
            preCourseResponseList.add(preCourseResponse);
        }
        return preCourseResponseList;
    }
}




